<?php


namespace app\common\controller;


use app\common\models\GroupMenuModel;
use app\common\models\MenuModel;
use app\admin\service\GroupMenuService;
use app\BaseController;
use think\App;
use think\facade\View;

class AdminBaseController extends BaseController
{
    public $menus;
    public $params;
    public $user;
    protected $service;
    public function __construct(App $app)
    {
        parent::__construct($app);
        // 获取用户
        $this->user = session('admin');
        if(!$this->user) {
            header("Location: ".url('/auth/login/index'));
            exit();
        }
        $this->menus = $this->initMenu($this->user['group']);
        $this->params = input();
    }

    /**
     * 初始化菜单
     */
    public function initMenu($groupId) {
        if(!$groupId) {
            // 超级管理员有全部的菜单
            $condition = [
                'del_flag' => 0,
                'is_menu' => 1
            ];
            $menuModel = new MenuModel();
            $data = $menuModel->where($condition)->order('sort asc')->select();
        }else {
            // 获得角色的菜单
            $condition = [
                'del_flag' => 0,
                'group_id' => $groupId,
            ];
            $groupMenuModel = new GroupMenuModel();
            $menuIdList = $groupMenuModel->where($condition)->field('menu_id')->select();
            // 获得角色的详细详细信息
            $menuData = [];
            foreach ($menuIdList as $val) {
                $menuData[] = $val['menu_id'];
            }
            if(count($menuData) == 0) {
                return [];
            }
            $where = [
                'del_flag' => 0,
                'id' => $menuData,
                'is_menu' => 1
            ];
            $menuModel = new MenuModel();
            $data = $menuModel->where($where)->order('sort asc')->select();
        }
        // 格式化菜单
        $data = self::prepareMenu($data);
        return $data;
    }

    public static function prepareMenu($param)
    {
        $parent = []; //父类
        $child = [];  //子类

        foreach($param as $key=>$vo){
            if($vo['pid'] == 0){
                $vo['href'] = '#';
//                $parent[] = $vo;
                $parent[] = [
                    'id' => $vo['id'],
                    'title' => $vo['title'],
                    'href' => '#',
                    'children' => []
                ];
            }else{
                $vo['href'] = url($vo['url']); //跳转地址
//                $child[] = $vo;
                $child[] = [
                    'title' => $vo['title'],
                    'href' => url($vo['url']),
                    'pid' => $vo['pid']
                ];
            }
        }

        foreach($parent as $key=> &$vo){
            foreach($child as $k=>$v){
                if($v['pid'] == $vo['id']){
                    $vo['children'][] = $v;
//                    $parent[$key]['child'][] = $v;
                }
            }
        }
        unset($child);
        return $parent;
    }

    public function index() {
        return view('index');
    }

    public function getData() {
        return $this->service->getData($this->params);
    }

    /**
     * 添加
     * @return \think\response\View
     */
    public function add() {
        if(request()->isPost()) {
            if(isset($this->params['file'])) {
                unset($this->params['file']);
            }
            return $this->service->add($this->params);
        }else {
            return view('add');
        }
    }

    /**
     * 编辑
     * @param $id
     * @return \think\response\View
     */
    public function edit($id) {
        if($this->request->isPost()) {
            if(isset($this->params['file'])) {
                unset($this->params['file']);
            }
            return $this->service->modify(['id' => $id],$this->params);
        }else {
            $data = $this->service->detail($id);
            return view('edit',[
                'data' => $data
            ]);
        }
    }
    /*
     * 删除
     */
    public function del($id) {
        return $this->service->del($id);
    }



}